﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI05B : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SI05BFormModel frmModel;
        private string sUserName;
        private bool isChooser = false;
        #endregion

        #region Constructor
        public SI05B()
        {
            InitializeComponent();
            initDefaultData();
        }

        public SI05B(Translator trans, string sUname, Form caller)
            : base(sUname, caller)
        {
            sUserName = (string.IsNullOrEmpty(sUname) ? "" : sUname);
            this.trans = trans;
            Owner = caller;
            InitializeComponent();
            initDefaultData();            
        } 
        #endregion

        #region Utility Methods
        private void loadMatCheckList()
        {
            string sNo = textBox_ProdExportNo.Text;
            string sfrom = dateTimePicker_FromDate.Value.ToString("yyyyMMdd");
            string sto = dateTimePicker_ToDate.Value.ToString("yyyyMMdd");
            dataGridView_ProdExportList.DataSource = frmModel.searchProdExportList(sNo, sfrom, sto);
        }

        private void initDefaultData()
        {
            frmModel = new SI05BFormModel();
            frmModel.Owner = this;
            trans.controlTranslate(this);

            dateTimePicker_ToDate.Value = DateTime.Today;
            dateTimePicker_FromDate.Value = DateTime.Today.AddDays(-1);

            frmModel.translator = this.trans;
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);
            PHIEU_XH_DTO temp = new PHIEU_XH_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_CHON));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_MS_PHIEU_XH));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_PHE_DUYET));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_KEM_HOA_DON));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_KHACH_HANG));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_DIA_CHI_KH));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_CREATION_TIME));
            properties.Add(temp.GetType().GetProperty(PHIEU_XH_DTO.N_CREATION_USER));

            frmModel.buildDataGrid(dataGridView_ProdExportList, properties, null);

            properties.Clear();

            PHIEU_XH_CT_DTO temp1 = new PHIEU_XH_CT_DTO();
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_SL_XUAT));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_TRONG_LUONG));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XH_CT_DTO.N_GHI_CHU));

            frmModel.buildDataGrid(dataGridView_ProdExportDetail, properties, null);
        }

        public void callChooser(bool b)
        {
            isChooser = b;
            button_ChooseProdEx.Enabled = b;
            button_DelProdExport.Enabled = !b;
            button_ModProdExport.Enabled = !b;
            button_NewProdExport.Enabled = !b;
        }

        public DataTable getSelectedProdEx()
        {
            return frmModel.getSelectedProd();
        }

        #endregion

        #region Event Methods
        private void button_SearchProdExport_Click(object sender, EventArgs e)
        {
            loadMatCheckList();
            //string sNo = dataGridView_ProdExportList.CurrentRow.Cells[PHIEU_XH_DTO.N_MS_PHIEU_XH].EditedFormattedValue.ToString();
            //dataGridView_ProdExportDetail.DataSource = frmModel.searchProdExportDetail(sNo);
        } 

        private void dataGridView_ProdExportList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string sNo = dataGridView_ProdExportList.CurrentRow.Cells[PHIEU_XH_DTO.N_MS_PHIEU_XH].EditedFormattedValue.ToString();
                dataGridView_ProdExportDetail.DataSource = frmModel.searchProdExportDetail(sNo);
            }
        }

        private void button_ModProdExport_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_MODIFY)))
            {
                if (dataGridView_ProdExportList.CurrentRow != null)
                {
                    SI05A newFrm = new SI05A(trans, sUserName, this);
                    newFrm.initExistData(dataGridView_ProdExportList.CurrentRow, frmModel.prodExportDetail);
                    newFrm.initDefaultData();
                    newFrm.Show();
                }
            }
        }

        private void button_NewProdExport_Click(object sender, EventArgs e)
        {
            SI05A newFrm = new SI05A(trans, sUserName, this);
            newFrm.initDefaultData();
            newFrm.Show();
        }

        private void button_ChooseProdEx_Click(object sender, EventArgs e)
        {
            if (isChooser && frmModel.DataList.Count > 0)
            {
                string sNo = dataGridView_ProdExportList.CurrentRow.Cells[PHIEU_XH_DTO.N_MS_PHIEU_XH].EditedFormattedValue.ToString();
                dataGridView_ProdExportDetail.DataSource = frmModel.searchProdExportDetail(sNo);
                this.Hide();
            }
        }

        private void dataGridView_ProdExportList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex == 0)
            {
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));
            }
        }

        private void button_DelProdExport_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (dataGridView_ProdExportList.CurrentRow != null)
                {
                    string selectProdEx = dataGridView_ProdExportList.CurrentRow.Cells[PHIEU_XH_DTO.N_MS_PHIEU_XH].EditedFormattedValue.ToString();
                    bool b1 = frmModel.removeProdExportDetail(selectProdEx), b2 = frmModel.removeProdExport(selectProdEx);
                    if (b1 || b2)
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        loadMatCheckList();
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_ProdExportList.CurrentRow != null)
            {
                int index = dataGridView_ProdExportList.CurrentRow.Index;
                PHIEU_XH_DTO curOb = new PHIEU_XH_DTO();
                curOb.KHACH_HANG = frmModel.prodExportList.Rows[index][PHIEU_XH_DTO.N_KHACH_HANG].ToString();
                curOb.DIA_CHI_KH = frmModel.prodExportList.Rows[index][PHIEU_XH_DTO.N_DIA_CHI_KH].ToString();
                curOb.KEM_HOA_DON = frmModel.prodExportList.Rows[index][PHIEU_XH_DTO.N_KEM_HOA_DON].ToString();
                curOb.CREATION_TIME = DateTime.Parse(frmModel.prodExportList.Rows[index][PHIEU_XH_DTO.N_CREATION_TIME].ToString());
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {

                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), curOb))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion        
    }
}
